package henu.graduation_design.onlineshop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import henu.graduation_design.adapter.CartAdapter;
import henu.graduation_design.tool.AnalyseCartJson;
import henu.graduation_design.util.JsonLoader;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CartActivity extends Activity {

	SharedPreferences sp;
	double totalPrice;

	ListView cartListView;
	TextView totalPriceTextView;
	TextView buyTextView;
	private JsonLoader jsonLoader = new JsonLoader();
	List<HashMap<String, Object>> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_cart);
		list=new ArrayList<HashMap<String,Object>>();
		cartListView=(ListView)findViewById(R.id.cart_list);
		totalPriceTextView=(TextView)findViewById(R.id.total_price);
		buyTextView=(TextView)findViewById(R.id.cart_buy);
		sp = getSharedPreferences("SP", MODE_PRIVATE);
		if (!sp.getBoolean("IsLogin", false)) {
			Intent intent = new Intent(CartActivity.this,
					LoginDialogActivity.class);
			startActivityForResult(intent, 0);
		} else {
			// 显示购物车
			showCart();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) {
		case RESULT_OK:
			boolean login = data.getBooleanExtra("isLogin", false);
			if (login) {
				// 显示购物车
				showCart();
			}
			break;

		default:
			Intent intent=new Intent(this,TabActivity.class);
			startActivity(intent);
			finish();
			break;
		}
	}
	
	private void showCart() {
		sp = getSharedPreferences("SP", MODE_PRIVATE);
		try {
			loadJson(getString(R.string.url_cart),
					sp,
					null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		buyTextView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent=new Intent(CartActivity.this,AddressActivity.class);
				ArrayList<Integer> iDList = new ArrayList<Integer>();
				for(Integer integer=0;integer<list.size();integer++)
				{
					iDList.add((Integer) list.get(integer).get("id"));
				}
				if(iDList.size()!=0){
					intent.putIntegerArrayListExtra("IDList", iDList);
					startActivity(intent);
					finish();
				}
			}
		});
	}

	private void loadJson(final String url, SharedPreferences spp,
			final Map<String,Object> mapEntry) {
		jsonLoader.postJson(url, spp, mapEntry,
				new JsonLoader.JsonObjectCallback() {
					public void jsonLoaded(JSONObject jsonObject) {
						list.clear();
						AnalyseCartJson analyse = new AnalyseCartJson(
								jsonObject);
						try {
							list=analyse.getList();
						} catch (JSONException e) {
							Toast.makeText(getApplicationContext(),
									"解析category的json数据发生异常", Toast.LENGTH_LONG)
									.show();
						}
						for (int i = 0; i < list.size(); i++) {
							totalPrice += Double.parseDouble(list.get(i).get("price").toString());
						}
						CartAdapter adapter = new CartAdapter(
								CartActivity.this, list);
						cartListView.setAdapter(adapter);
						totalPriceTextView.setText("总金额："+totalPrice);
					}
				});
	}                                                                                           

}
